package com.cec.service.admin.impl;

import com.cec.dto.CategoryDTO;
import com.cec.entity.Category;
import com.cec.exception.CategoryException;
import com.cec.exception.OperationDataException;
import com.cec.mapper.admin.ActivityMapper;
import com.cec.mapper.admin.CategoryMapper;
import com.cec.mapper.admin.InfoMapper;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.service.admin.ActivityService;
import com.cec.service.admin.CategoryService;
import com.cec.service.admin.InfoService;
import com.cec.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private InfoMapper infoMapper;
    @Override
    public Result addActivityCategory(CategoryDTO categoryDTO) {

        if (!checkActivityCategory(categoryDTO.getName()))
            throw new CategoryException(ResultCode.CATEGORY_ERROR);

//log.info("checkActivityCategory(categoryDTO.getName())-->{}",checkActivityCategory(categoryDTO.getName()));
        int i = categoryMapper.saveActivityCategory(categoryDTO);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result addInfoCategory(CategoryDTO categoryDTO) {

        if (!checkInfoCategory(categoryDTO.getName()))
            throw new CategoryException(ResultCode.CATEGORY_ERROR);

        int i = categoryMapper.saveInfoCategory(categoryDTO);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result updateActivityCategory(CategoryDTO categoryDTO) {
        if (!checkActivityCategory(categoryDTO.getName()))
            throw new CategoryException(ResultCode.CATEGORY_ERROR);
        int i = categoryMapper.updateActivityCategory(categoryDTO);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result updateInfoCategory(CategoryDTO categoryDTO) {
        if (!checkInfoCategory(categoryDTO.getName()))
            throw new CategoryException(ResultCode.CATEGORY_ERROR);
        int i = categoryMapper.updateInfoCategory(categoryDTO);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result deleteActivityCategory(Integer id) {
        int i = categoryMapper.deleteActivityCategory(id);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result deleteInfoCategory(Integer id) {
        int i = categoryMapper.deleteInfoCategory(id);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    public boolean checkActivityCategory(String name) {
        List<CategoryVO> categoryVOList = activityMapper.queryActivityTypeList();
        for (CategoryVO categoryVO : categoryVOList) {
            if (categoryVO.getName().equals(name)) {
                return false;
            }
        }
        return true;
    }

    public boolean checkInfoCategory(String name) {
        List<CategoryVO> categoryVOS = infoMapper.queryInfoType();
        for (CategoryVO categoryVO : categoryVOS) {
            if (categoryVO.getName().equals(name)) {
                return false;
            }
        }
        return true;
    }
}
